﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EzyGoEzySystem.Models;
using EzyGoEzySystem.Interfaces;
using EzyGoEzySystem.Services;

namespace EzyGoEzySystem.Controllers
{
    public class HomeController : Controller
    {
        EzyGoEzyDBContext dbContext = new EzyGoEzyDBContext();

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            //create accounts
            var firstAccount = new Account
            {
                Name = "Nguyen Quang Phuc",
                //Address = "#22-203, Blk 102, Jurong East Street 12, Singapore 600102",
                //DateOfBirth = new DateTime(1987, 12, 10),
                Email = "nguyenquangphuc@gmail.com",
                //Gender = "Male",
                //NRIC = "S04865208",
                PhoneNumber = "98349721",
                Password = "itsasecret",
                Balance = 0,
                PaymentMethod = "Postpaid",
                MacAddress = "00:25:48:EA:8D:77"
            };
            var secondAccount = new Account
            {
                Name = "Tran Binh Thanh",
                //Address = "#22-203, Blk 102, Jurong East Street 12, Singapore 600102",
                //DateOfBirth = new DateTime(1987, 12, 10),
                Email = "tranbinhthanh@gmail.com",
                //Gender = "Male",
                //NRIC = "S04865208",
                PhoneNumber = "84456432",
                Password = "itsasecret",
                Balance = 0,
                PaymentMethod = "Postpaid",
                MacAddress = "00:19:4F:0D:49:E3"
            };

            var accounts = from acct in dbContext.Accounts
                           select acct;
            
            //// add accounts
            //dbContext.Accounts.Add(firstAccount);
            //dbContext.Accounts.Add(secondAccount);

            //// commit to db
            //dbContext.SaveChanges();

            //var account = new Account
            //{
            //    Name = "Le Hong Trieu",
            //    Address = "#22-203, Blk 102, Jurong East Street 12, Singapore 600102",
            //    DateOfBirth = new DateTime(1987, 12, 10),
            //    Email = "lehongtrieu@gmail.com",
            //    Gender = "Male",
            //    NRIC = "S04865208",
            //    PhoneNumber = "98349721",
            //    Password = "itsasecret",
            //    Balance = 0,
            //    PaymentMethod = "Postpaid"
            //};

            //dbContext.Accounts.Add(account);
            //dbContext.SaveChanges();

            //test method BoardBus()


            //List<Account> accountList = new List<Account>();
            ////add into list
            //accountList.Add(firstAccount);
            //accountList.Add(secondAccount);

            //BoardAlightService test = new BoardAlightService();
            //test.BoardBus(accountList, new DateTime(2011, 12, 12), dbContext);
            //var account1 = from acct in dbContext.Accounts
            //              select acct;

            //BoardBus(IEnumerable<Account> listAccounts, DateTime boardTime);

            //var travelEntry = new TravelJournalEntry
            //{
            //    BoardTime = DateTime.Now,
            //    AlightTime = DateTime.Now.AddHours(1),
            //    Fare = 0,
            //    Account = account1.First<Account>()
            //};

            //dbContext.TravelJournalEntries.Add(travelEntry);
            //dbContext.SaveChanges();


            //var travelEntries = from acct in dbContext.TravelJournalEntries
            //                    select acct;
            //var returnedAcct = travelEntries.Last();

            //ILoginService loginService = new LoginService();
            //var accounts = loginService.FetchAccount(new string[] { "98349721" });

            //return View();

            //dbContext.Dispose();
            return View(accounts.ToList());
        }

        public ActionResult About()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Account account)
        {
            if (ModelState.IsValid)
            {
                dbContext.Accounts.Add(account);
                dbContext.SaveChanges();

                return RedirectToAction("Index");
            }

            return View(account);
        }
    }
}
